﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;

namespace DataAccess.NHibernate
{
    public class NHUnitOfWork
    {
        protected ISession NHContext;
        public NHUnitOfWork(ISession nHContext)
        {
            this.NHContext = nHContext;
        }
                  
        public void RegisterAmended(object entity, IUnitOfWorkRepository unitofWorkRepository)
        {
            NHContext.SaveOrUpdate(entity);
        }

        public void RegisterNew(object entity, IUnitOfWorkRepository unitofWorkRepository)
        {
            NHContext.Save(entity);
        }

        public void RegisterRemoved(object entity, IUnitOfWorkRepository unitofWorkRepository)
        {
            NHContext.Delete(entity);
        }   

        public void Commit()
        {
            using (ITransaction transaction = NHContext.BeginTransaction())
            {
                try
                { transaction.Commit(); }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
    }
}